package pl.piomin.service.blockchain.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import pl.piomin.service.blockchain.model.BlockchainTransaction;
import pl.piomin.service.blockchain.service.BlockchainService;

import java.io.IOException;

@RestController
public class BlockchainController {

    private final BlockchainService service;

    public BlockchainController(BlockchainService service) {
        this.service = service;
    }

    /**
     * 处理区块链交易请求
     *
     * 该方法接收一个区块链交易对象，作为HTTP POST请求的主体，并将其提交给服务层进行处理
     * 主要用于将交易信息添加到区块链中
     *
     * @param transaction 包含交易详情的区块链交易对象，通过请求体传递
     * @return 返回处理后的区块链交易对象，包含可能的更新或状态信息
     * @throws IOException 如果在处理过程中发生I/O错误，则抛出此异常
     */
    @PostMapping("/transaction")
    public BlockchainTransaction execute(@RequestBody BlockchainTransaction transaction) throws IOException {
        return service.process(transaction);
    }

}
